<template>
     <van-nav-bar title="地址管理" left-arrow @click-left="towhich">
    <template #right>
      <van-popover v-model:show="showPopover" :actions="actions" @select="onSelect" :offset="[-60,8]">
        <template #reference>
          <van-icon name="plus" size="30" @click-right="onClickRight" />
        </template>
      </van-popover>
    </template>
  </van-nav-bar>

    <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="onAdd" @edit="onEdit($event)"
        add-button-text="确认地址" />
</template>

<script setup lang="ts">
import { ref, onMounted} from 'vue';
import { showToast } from 'vant';
import { useRouter,  } from 'vue-router';
import { GetUserAds, DeleteAddress, AddAdress } from '@/api/api';
import { useUserInfoStore } from '@/stores/useInfo';


const store = useUserInfoStore()
const userid = store.getUserId
const router = useRouter()
const chosenAddressId = ref(0)
const list = ref<any[]>([])
const province = ref<any[]>([])
const city = ref<any[]>([])
const county = ref<any[]>([])
const addressDetail = ref<any[]>([])

const showPopover = ref(false)
const actions = [
  { text: '添加地址' }
]
const onSelect = (action: any) => {
  console.log(action.text);
    router.push('/addadres')
}

const onClickRight = () => {

}

const towhich = () => {
    const path = localStorage.getItem("fullpath") as string
    router.push(path)
}

const getadslist = async () => {//获取地址列表
    const params = { userid: userid }
    const res = await GetUserAds(params)
    // console.log(res);

    if (res.data.code === '200') {
        if (res.data.data.length == 0) {
            router.push('/addadres')
        } else {
            res.data.data.forEach((i: any, index: any) => {
                list.value.push({
                    id: index + 1,
                    name: i.name,
                    tel: i.tel,
                    address: i.province + i.city + i.county + i.addressDetail,
                    isDefault: i.isDefault,
                    adressid: i.addressid
                });
                province.value.push(i.province);
                city.value.push(i.city);
                county.value.push(i.county)
                addressDetail.value.push(i.addressDetail)
            })
        }
    }
}


onMounted(() => {
    getadslist()
})

const deletads = async () => {//删除地址
    const index = chosenAddressId.value - 1
    const adsid = list.value[index].adressid
    const result = await DeleteAddress({ addressid: adsid })
    if (result.data.code === '200') {
        console.log('删除成功');
    }
}

const addads = async () => {//添加地址
    const index2 = chosenAddressId.value - 1
    console.log(userid);
    const params2 = {
        userid: userid,
        name: list.value[index2].name,
        tel: list.value[index2].tel,
        province: province.value[index2],
        city: city.value[index2],
        county: county.value[index2],
        addressDetail: addressDetail.value[index2],
        isDefault: true
    }
    const result2 = await AddAdress(params2)
    console.log(result2);
    if (result2.data.code === '200') {
        showToast({
            message: '修改地址成功',
            position: 'middle'
        })
    } else {
        return false
    }

}

const onAdd = async () => {//确认地址
    console.log(chosenAddressId.value);
    if (chosenAddressId.value != 0) {
        await deletads()
        await addads()
        router.back()
    }else{
        showToast({
            message:'请选择地址',
            position:'middle'
        })    
    }
}
const onEdit = ($event: any) => {//操作地址
    router.push({
        path: '/adedit',
        query: {
            id: $event.adressid
        }
    })
};

</script>

<style lang="scss" scoped></style>